package com.youfang.sixthmedia;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.gzit.adapter.CustomerListAdapter;
import com.gzit.common.async.AsynCallback;
import com.gzit.utils.AndroidUtils;
import com.youfang.sixthmedia.common.YoufangContext;
import com.youfang.sixthmedia.message.GetCityRequest;
import com.youfang.sixthmedia.message.GetCityResponse;
import com.youfang.sixthmedia.message.model.City;
import com.youfang.sixthmedia.net.Net;
import com.youfang.sixthmedia.net.Response;
import com.youfang.sixthmedia.widget.IndexableListView;

/**
 * 选择城市
 * 
 * @author teddy
 * 
 */
public class CityActivity extends Activity {

	private IndexableListView listview;
	private CityAdapter adapter;

	private class CityAdapter extends CustomerListAdapter<City> implements
			SectionIndexer {

		public CityAdapter(Context context) {
			super(context);
			this.setItemLayout(R.layout.city_item);
		}

		private String mSections = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";

		@Override
		protected void render(final City item, View view) {

			TextView name = (TextView) view.findViewById(R.id.name);
			name.setText(item.name);

			// 标示选中状态
			ImageView sel = (ImageView) view.findViewById(R.id.selectIndicator);

			City curCity = YoufangContext.getCity();
			if (curCity.cityid==item.cityid) {
				sel.setImageResource(R.drawable.tick);
			} else {
				sel.setImageDrawable(null);
			}

		}

		@Override
		public int getPositionForSection(int section) {
			// If there is no item for current section, previous section will be
			// selected
			// for (int i = section; i >= 0; i--) {
			// for (int j = 0; j < getCount(); j++) {
			// if (i == 0) {
			// // For numeric section
			// for (int k = 0; k <= 9; k++) {
			// if (StringMatcher.match(
			// String.valueOf(getItem(j).charAt(0)),
			// String.valueOf(k)))
			// return j;
			// }
			// } else {
			// if (StringMatcher.match(
			// String.valueOf(getItem(j).charAt(0)),
			// String.valueOf(mSections.charAt(i))))
			// return j;
			// }
			// }
			// }
			return 0;
		}

		@Override
		public int getSectionForPosition(int position) {
			return 0;
		}

		@Override
		public Object[] getSections() {
			String[] sections = new String[mSections.length()];
			for (int i = 0; i < mSections.length(); i++)
				sections[i] = String.valueOf(mSections.charAt(i));
			return sections;
		}

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.city_activity);

		adapter = new CityAdapter(this);
		listview = (IndexableListView) findViewById(R.id.listview);
		listview.setAdapter(adapter);
		listview.setFastScrollEnabled(true);

		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				selectedCity(adapter.getItem(position));
			}
		});

		remoteCity();

	}

	private void selectedCity(City city) {

		// 保存配置
		YoufangContext.saveCity(city);

		// 返回
		this.setResult(Activity.RESULT_OK);
		this.finish();

	}

	private void remoteCity() {

		Net.doHttpRemote(this, new GetCityRequest(),
				new GetCityResponse(), new AsynCallback<Response>() {

					@Override
					public void callback(Response r) {
						if (!r.isOk()) {
							AndroidUtils.toastShort("城市列表更新失败");
							return;
						}

						GetCityResponse resp = (GetCityResponse) r;
						adapter.setmObjects(resp.getCitys());
						adapter.notifyDataSetChanged();

					}
				});

	}

}
